package com.ptswitch.material.pages.contract;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.util.EMailUtil;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.util.HlogConstants;
import com.ptswitch.material.util.MaterialUtil;

public class DeleteContract extends MaterialBasePage {

    @Inject
    private ContractService contractSer;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object delete(String id) {
        Contract contract = contractSer.findById(id);
        checkUserIsEqual(contract.getAuditor());
        contractSer.delete(contract);
        hlog(HlogConstants.deleteContract, contract);
        //send
        EMail email = getMaterialUtil().newEMail();
        email.addTo(contract.getCreater().getEmail());
        email.setSubject(contract.getAuditor().getRealname() + "已删除了合同：" + contract.getCode());
        email.addText(MaterialUtil.buildMessage(contract));
        EMailUtil.send(email);
        return ListContract.class;
    }
}
